/*
 * $Id: SunReferenceEnhancer.java,v 1.6 2003/10/20 05:59:26 jackknifebarber Exp $
 */

package com.triactive.jdo.enhance;

import java.io.File;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * This is an implementation of the Enhancer that wraps Sun's JDO Reference
 * Implementation class endhancer.
 *
 * @version $Revision: 1.6 $
 */
public class SunReferenceEnhancer
  extends Enhancer
{
    private final Collection jdoFiles;
    private final String destDir;
    private final boolean verbose;


    private SunReferenceEnhancer(String destDir, boolean verbose, Collection jdoFiles)
    {
        super();

        this.destDir = destDir;
        this.verbose = verbose;
        this.jdoFiles = jdoFiles;
    }


    /**
     * This method accepts a list of jdo files that should be enhanced by
     * the Sun Reference Implementation enhancer.
     *
     * @param classNames  The list of class names to be enhanced
     */
    protected int callExternalEnhancer(String[] classNames) throws Exception
    {
        int returnCode = 0;

        if (classNames.length > 0)
        {
            ArrayList args = new ArrayList();

            args.add("-f");   // Force writing of .class files.
            args.add("-d");
            args.add(destDir);

            if (verbose)
                args.add("-v");

            args.addAll(jdoFiles);

            for (int i = 0; i < classNames.length; ++i)
            {
                String name = classNames[i];
                System.out.println("Enhancing class " + name);
                System.out.println("destDir " + destDir);

                args.add(new File(destDir, name.replace('.', '/') + ".class").toString());
            }

            com.sun.jdori.enhancer.Main main = new com.sun.jdori.enhancer.Main();
            returnCode = main.process((String[])args.toArray(new String[args.size()]));
        }

        return returnCode;
    }


    /**
     * Called when the class is invoked from the command line.
     *
     * @param args
     *   A list of JDO metadata files (*.jdo files). The list may optionally
     *   be prefixed by a -l flag followed by the name of a file to which
     *   the list of enhanced classes will be written to.  The -d argument
     *   is also accepted to specify the destination directory to write the
     *   class files to.  The -v flag makes the enhancer run with verbose
     *   output.
     */
    public static void main(String[] args) throws Exception
    {
        String classListFile = null;
        String destDir = ".";//era "."
        boolean verbose = false;
        ArrayList jdoFiles = new ArrayList();

        for (int i=0; i<args.length; i++)
        {
            if (args[i].equals("-l"))
            {
                classListFile = args[++i];
            }
            else if (args[i].equals("-d"))
            {
                destDir = args[++i];
            }
            else if (args[i].equals("-v"))
            {
                verbose = true;
            }
            else
            {
                jdoFiles.add(args[i]);
            }
        }

        List classNames = getOrderedClassNames((String[])jdoFiles.toArray(new String[jdoFiles.size()]));

        if (classListFile != null)
            writeClassListFile(classListFile, classNames);

        System.exit(new SunReferenceEnhancer(destDir, verbose, jdoFiles).enhance(classNames));
    }
}
